<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Service\RoleService;
use Illuminate\Http\Request;
use App\Http\Service\AdminUserService;
use Auth;

class UserController extends Controller
{

    public function index(Request $request)
    {
        $request->input('pageSize')?$pageSize=$request->input('pageSize'):$pageSize=20;
        $request->input('searchVal')?$searchVal=$request->input('searchVal'):$searchVal=null;
        $userList = AdminUserService::getUserListPageInit($pageSize,$searchVal);
        return view('admin.user.index',[
            'pageSize'=>$pageSize,
            'searchVal'=>$searchVal,
            'users'=>$userList,
        ]);
    }

    public function create(Request $request){
        $roles = RoleService::getAllRoles();
        return view('admin.user.create',[
            'roles'=>$roles,
        ]);
    }

    public function doCreate(Request $request){
        $this->validateUser($request);
        $info=$request->input('info');
        $info['password']=bcrypt($request->input('password'));
        $roles = $request->input('roles');
        $items = $request->get('items',[]);
        if(AdminUserService::createOneUser($info,$roles,$items)) {
            return redirect('/admin/user/index');
        }else{
            return redirect()->back();
        }
    }

    public function edit(Request $request){
        $uid = $request->input('uid');
        $roles = RoleService::getAllRoles();
        $info = AdminUserService::getOneUser($uid);
        return view('admin.user.edit',[
            'roles'=>$roles,
            'info'=>$info,
        ]);
    }

    public function doEdit(Request $request){
        $info = $request->input('info');
        $roles = $request->input('roles');
        $items = $request->get('items',[]);

        if(AdminUserService::updateRoles($info['id'],$roles,$items)){
            return redirect('/admin/user/index');
        }else{
            return redirect()->back();
        }
    }

    public function delete(Request $request){
        $uid=$request->input('uid');
        if(AdminUserService::deleteOneUser($uid)){
            return response()->json([
                'code' => 200,
                'message' => '操作成功！'
            ]);
        }
        else{
            return response()->json([
                'code' => 500,
                'message' => '操作失败！请重试!'
            ]);
        }
    }

    public function resetPasswd(Request $request){
        $uid=$request->input('uid');
        if(AdminUserService::resetPasswd($uid)){
            return response()->json([
                'code' => 200,
                'message' => '操作成功！'
            ]);
        }
        else{
            return response()->json([
                'code' => 500,
                'message' => '操作失败！请重试!'
            ]);
        }
    }

    public function modifyPasswd(Request $request){
        $info = AdminUserService::getOneUser(Auth::user()->id);
        return view('admin.user.resetPasswd',[
            'info'=>$info,
        ]);
    }

    public function doModifyPasswd(Request $request){
        $this->validateUser($request);
        $info['id'] = AdminUserService::getOneUser(Auth::user()->id);
        $info['password']=bcrypt($info['password']);
        if(AdminUserService::modifyPasswd($info['id'],$info['password'])) {
            return redirect('/admin/user/index');
        }else{
                return redirect()->back();
        }
    }

    private function validateUser(Request $request){
        return $this->validate($request,[
            'info.name'=>'required',
            'info.email'=>'required|email|unique:users,email',
            'password'=>'required|min:6|max:20|confirmed',
            'password_confirmation'=>'required|min:6|max:20',
        ],[
            'required'=>':attribute不能为空',
            'integer'=>':attribute必须为整数',
            'email'=>':attribute格式不正确',
            'min'=>':attribute最小长度为6位',
            'max'=>':attribute最大长度为20位',
            'confirmed'=>'两次密码不一致',
            'unique'=>'邮箱已经注册'
        ],[
            'info.name'=>'用户名',
            'info.email'=>'邮箱',
            'password'=>'密码',
            'password_confirmation'=>'确认密码'
        ]);
    }


}
